package com.usian.behavior.listener;

import com.alibaba.fastjson.JSON;
import com.usian.behavior.service.ApFollowBehaviorService;
import com.usian.model.behavior.dtos.FollowBehaviorDto;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class FollowBehaviorListener {

    @Autowired
    ApFollowBehaviorService apFollowBehaviorService;



    @KafkaListener(topics = {"followBehavior"})
    public void followBehavior(ConsumerRecord<?,?> record){
        Optional<? extends  ConsumerRecord<?,?>> optional =Optional.ofNullable(record);
        if ( optional.isPresent()){
            String dto =(String) record.value();
            FollowBehaviorDto followBehaviorDto = JSON.parseObject(dto, FollowBehaviorDto.class);
            System.out.println("关注行为获取消息"+followBehaviorDto.toString());
            apFollowBehaviorService.saveFollowBehavior(followBehaviorDto);
        }
    }
}
